const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId:0,
    orderData:"",
    phone:""
  },
  toShop(e) {
    wx.navigateTo({
      url: '/pages/shop/shop?id=' + this.data.orderData.shop.id
    })
  },
  //去支付 TODO
  payOrder(e) {
    let that = this;
    wx.request({
      url: app.buildUrl('/pay/pay-order'),
      header: app.buildHeader(),
      data: {
        order_id: that.data.orderId
      },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 200) {
          wx.requestPayment({
            'timeStamp': res.data.data.timeStamp + '',
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': res.data.data.signType,
            'paySign': res.data.data.paySign,
            success(res) {
              app.showToast('支付成功', 'success')
              wx.redirectTo({
                url: '../success/success?id=' + that.data.orderId
              })
            },
            fail(res) { }
          })
        } else {
          app.showToast(res.data.msg)
        }
      }
    })
  },
  // 申请售后
  apply(){
    wx.navigateTo({
      url: '../apply_after_sale/apply_after_sale?id=' + this.data.orderData.id,
    })
  },
  // 联系商家
  call(){
    let that = this;
    wx.makePhoneCall({
      phoneNumber: '' + that.data.orderData.shop.shop_phone
    })
  },
  // 再次下单
  again() {
    wx.redirectTo({
      url: '../detail/detail?id=' + this.data.orderData.active_id
    })
  },
  // 去支付
  // goPay(){
  //   wx.redirectTo({
  //     url: `../pay/pay?id=${this.data.orderData.goods_attr_id}&num=${this.data.orderData.goods_num}&type=${this.data.orderData.order_type }`
  //   })
  // },
  // 删除订单
  delOrder(){
    let that = this;
    wx.showModal({
      title: '提示',
      content: '删除后订单后不可恢复',
      success(res){
        if(res.confirm){
          wx.showLoading({
            title: '删除中...',
          })
          wx.request({
            url: app.buildUrl('/order/del-order'),
            header: app.buildHeader(),
            method: 'GET',
            data: {
              order_id: that.data.orderId
            },
            success(res) {
              wx.hideLoading()
              app.showToast(res.data.msg);
              if(res.data.code == 200){
                setTimeout(() => {
                  wx.redirectTo({
                    url: '../order_list/order_list'
                  });
                }, 1000)
              }
            }
          });
        }
      }
    })
  },
  // 复制
  copy(){
    let that = this;
    wx.setClipboardData({
      data: that.data.orderData.order_no,
      success(res) {
        app.showToast('复制成功', "success");
      }
    })
  },
  copywl() {
    let that = this;
    wx.setClipboardData({
      data: that.data.orderData.transport_no,
      success(res) {
        app.showToast('复制成功', "success");
      }
    })
  },
  //售后详情
  lookup(e) {
    let id = e.target.dataset.id;
    wx.navigateTo({
      url: '/pages/after_sale/after_sale?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      orderId: options.id || 95
    })
    wx.showLoading({
      title: '获取订单信息...',
    })
    // 获取电话
    // wx.request({
    //   url: app.buildUrl('/user/get-shop-phone'),
    //   header: app.buildHeader(),
    //   success: res => {
    //     that.setData({
    //       phone: res.data.data
    //     })
    //   }
    // })
    // 获取订单信息
    wx.request({
      url: app.buildUrl('/order/get-order-info'),
      header:app.buildHeader(),
      data:{
        id: that.data.orderId
      },
      success(res){
        wx.hideLoading()
        if(res.data.code == 200){
          let data = res.data.data;
          app.transformTime(data.created_time * 1000 , (options)=>{
            data.created_time = `${options.Y}-${options.M}-${options.D} ${options.h}:${options.m}:${options.s}`
          })
          that.setData({
            orderData: data
          })
        }else{
          app.showToast(res.data.msg)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  }
})